# Paul's Awesome Top-Down Thing!
import random, pygame, sys, time
from pygame.locals import *

FPS = 15
WINDOWWIDTH = 640
WINDOWHEIGHT = 480
CELLSIZE = 20
CELLMARGIN = 2

assert WINDOWWIDTH % CELLSIZE == 0 , "Window width must be multiple of cell size."
xTiles = WINDOWWIDTH / CELLSIZE

assert WINDOWHEIGHT % CELLSIZE == 0 , "Window height must be multiple of cell size."
yTiles = WINDOWHEIGHT / CELLSIZE

CENTERY = WINDOWHEIGHT / 2 
CENTERX = WINDOWWIDTH / 2

SPLASHRECT_WIDTH = WINDOWWIDTH * .6
SPLASHRECT_HEIGHT = WINDOWHEIGHT * .6

SPLASHRECT_TOP = CENTERY - SPLASHRECT_HEIGHT / 2
SPLASHRECT_LEFT = CENTERX - SPLASHRECT_WIDTH / 2

# Color Constants
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

def showSplash():
    
    pygame.draw.rect(DISPLAYSURF, GREEN, (SPLASHRECT_LEFT, SPLASHRECT_TOP, SPLASHRECT_WIDTH, SPLASHRECT_HEIGHT))
    exit = False
    start = time.clock()
    
    while((exit == False) and ((time.clock() - start) < 5)):
        if(checkForKeyPress()):
            exit = True
        if(checkForMousePress()):
            exit = True
        pygame.display.update()
        
def checkForKeyPress():
    if len(pygame.event.get(QUIT)) > 0:
        terminate()
    keyUpEvents = pygame.event.get(KEYUP)
    if len(keyUpEvents) == 0:
        return None
    return keyUpEvents[0].key

def checkForMousePress():
    if len(pygame.event.get(QUIT)) > 0:
        terminate()
    mouseUpEvents = pygame.event.get(MOUSEBUTTONUP)
    if len(mouseUpEvents) == 0:
        return None
    return mouseUpEvents[0].button
    
def terminate():
    pygame.quit()
    sys.exit()

def runGame():
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
            
            
def drawGrid(surface):
    for x in range(0, WINDOWWIDTH, CELLSIZE):
        pygame.draw.line(surface, WHITE, (x, 0), (x, WINDOWHEIGHT), CELLMARGIN)
    for y in range(0, WINDOWHEIGHT, CELLSIZE):
        pygame.draw.line(surface, WHITE, (0, y), (WINDOWWIDTH, y), CELLMARGIN)
        
#     Lol why did I do it like this?
#     tile = Rect(0, 0, CELLSIZE, CELLSIZE)
#     for x in range(0, xTiles):
#         for y in range(0, yTiles):
#             pygame.draw.rect(surface, WHITE, tile, CELLMARGIN)
#             tile.move_ip(0, CELLSIZE)
#         tile.move_ip(CELLSIZE, -(yTiles * CELLSIZE))



# Set-up
pygame.init()
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption("Snake")

showSplash()
DISPLAYSURF.fill(BLACK)
drawGrid(DISPLAYSURF)

while True:
    runGame()
    pygame.display.update()
            
